package com.kedacom.demo.modularity.email;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.kedacom.demo.modularity.core.StringList;

@Service("emailService")
public class EmailServiceImpl implements EmailService {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected Map<Long, Email> emails = new LinkedHashMap<Long, Email>();

	public EmailServiceImpl() {
		init();
	}

	protected void init() {
		emails.put(1L, new Email(1L, "test mail", "这是一封测试邮件", "zhiyou@test.com", "guojing@test.com"));
		emails.put(2L, new Email(2L, "您在致友微博有新的未读消息", "您有3位新粉丝", "zhiyou@test.com", "huangrong@test.com"));
		emails.put(3L, new Email(3L, "您在致友微博有新的未读消息", "您有5条新回复", "zhiyou@test.com", "guojing@test.com"));
		emails.put(4L, new Email(4L, "您在致友微博有新的未读消息", "您有2条@提到你的消息", "zhiyou@test.com", "huangrong@test.com"));
	}

	public void queue(Email email) {
		Assert.notNull(email);
		Assert.notNull(email.getId());
		Assert.hasText(email.getSubject());
		Assert.hasText(email.getFrom());
		Assert.hasText(email.getTo());

		emails.put(email.getId(), email);
		logger.debug("queue email {} success.", email);
	}

	public void send() {
		logger.debug("send queued emails success.");
	}

	public List<Email> listAll() {
		logger.debug("list all emails.");
		return new ArrayList<Email>(emails.values());
	}

	public StringList listTitles() {
		logger.debug("list all email titles.");
		List<String> titles = new ArrayList<String>();
		for (Email email : emails.values()) {
			titles.add(email.getSubject());
		}
		return new StringList(titles);
	}

}
